define(['zepto', 'sui', 'utils', 'wxshare'], function($, utils, wxshare) {
  return {
    init: function(data) {
      this.roundId = data.roundId;
      this.batchId = data.batchId;
      this.breakfasts = data.breakfasts;
      this.lunchs = data.lunchs;
      this.dinners = data.dinners;
      this.snacks = data.snacks;
      
      this.initClick();
      
      this.initPicker();
    },
    
    initPicker: function() {
        var that = this;
        $(".diet-modify, .set-meal").each(function(e){
            
            var type = $(this).data("type");
            var diets = that.initPickerData(type);
            
            var dietDisplay = new Array(),
                dietValue = new Array();
            $.each(diets, function(k, diet) {
                dietDisplay[k] = diet.name;
                dietValue[k] = diet.id;
            })
            
          $(this).picker({
                toolbarTemplate: '<header class="bar bar-nav">\
                    <button class="button button-link pull-right close-picker">确定</button>\
                    <h1 class="title">饮食</h1>\
                    </header>',
                    cols: [
                      {
                        textAlign: 'center',
                        values: dietValue,
                        displayValues: dietDisplay
                      }
                    ],
                    formatValue: function(picker, value, displayValue) {
                       that.value = value[0];
                       return displayValue
                    },
                      onClose: function(e) {
                        var $clicker = e.input
                        if($($clicker).hasClass("set-meal")) {
                          
                          var snackType = $($clicker).data("snack");
                          
                          if(type === 'snack') {
                            that.createDiet("snacks", snackType, $clicker);
                        } else {
                            that.createDiet("diets", snackType, $clicker);
                        }
                        } else {
                          var dietId = $($clicker).data("diet");
                          var templateId = $($clicker).data("template");
                          
                          if(parseInt(that.value) !== templateId) {
                              that.editDiet(dietId, $clicker)
                          }
                        }
                      }
                  });
        })
    },
    
    initPickerData: function(type) {
        var that = this;
        var diets;
        if(type === 'breakfast') {
            diets = that.breakfasts;
        } else if(type === 'lunch') {
            diets = that.lunchs;
        } else if(type === 'dinner') {
            diets = that.dinners;
        } else if(type === 'snack') {
            diets = that.snacks;
        }
        
        return diets;
    },
    
    
  createDiet: function(url, type, clicker) {
    if(url === 'snacks') {
        var meal = $('textarea[name="' + type + 'Description"]').val().trim();
        if(!meal) {
            $.toast("请先添加正餐！");
            return false;
        }
    }
    var that = this;
    $.ajax({
        url: window.__CTX__.root + '/rounds/' + that.roundId + '/batch-coursewares/' + that.batchId + '/' + url,
        type: 'POST',
        data: {
            'dietaryProgramTemplateId': that.value,
            'snacksType': type
        },
        success: function(res) {
            var res = JSON.parse(res);
            if(res.code == 0) {
              window.location.reload()
            }
        },
        fail: function() {
            $.toast("请求失败")
        }
    })
  },
  
  editDiet: function(dietId, clicker) {
      var that = this;
      $.ajax({
          url: window.__CTX__.root + '/rounds/' + that.roundId + '/batch-coursewares/' + that.batchId + '/diets/' + dietId,
          type: 'POST',
          data: {
              'dietaryProgramTemplateId': that.value,
              '_method': 'PATCH'
          },
          success: function(res) {
              var res = JSON.parse(res);
              if(res.code == 0) {
            	  window.location.reload()
              }                            
          },
          fail: function() {
              $.toast("请求失败")
          }
      })
  },
  
    initClick: function() {
        
        var that = this;
        
        $("textarea").on('change', function() {
            $(".save-diet").css("display", "block");
        })
        
        
        
        $(document).on('click', '.save-diet', function() {
        
            var breakfastDescription = $("textarea[name='breakfastDescription']").val().trim();
            var lunchDescription = $("textarea[name='lunchDescription']").val().trim();
            var dinnerDescription = $("textarea[name='dinnerDescription']").val().trim();
            var breakfastSnacksDescription = $("textarea[name='breakfastSnacksDescription']").val().trim();
            var lunchSnacksDescription = $("textarea[name='lunchSnacksDescription']").val().trim();
            var dinnerSnacksDescription = $("textarea[name='dinnerSnacksDescription']").val().trim();
            
            if(!breakfastDescription || !lunchDescription || !dinnerDescription) {
              $.toast("早中晚饮食不能为空");
              return false;
            }
            
            var data = {
             'breakfastDescription':breakfastDescription,
             'lunchDescription':lunchDescription,
             'dinnerDescription':dinnerDescription,
             'breakfastSnacksDescription':breakfastSnacksDescription,
             'lunchSnacksDescription':lunchSnacksDescription,
             'dinnerSnacksDescription':dinnerSnacksDescription,
             '_method': "PATCH"
            }
            
            $.ajax({
                url: window.__CTX__.root + '/rounds/' + that.roundId + '/batch-coursewares/' + that.batchId,
                type: 'POST',
                data: data,
                success: function(res){
                    var res = JSON.parse(res);
                    if(res.code == 0) {
                        $.toast("修改成功");
                        setTimeout(function(e){
                            window.location.reload();
                        }, 1000)
                    }   
                },
                fail: function() {
                    $.toast("请求失败")
                }
            })
        })
        
        
    }
    
  }
})